namespace Peer.Web.Controls.Upload
{
    using System;
    using System.Collections;
    using System.IO;
    using System.Text;
    using System.Web;

    internal class WebbRequestStream : IDisposable
    {
        private long i_boundarySize;
        private byte[] m_boundaryFlag;
        private int m_boundaryIndex = 2;
        private HttpContext m_content = null;
        private int m_contentIndex = 0;
        private ArrayList m_contentTextBody = new ArrayList();
        private FileStatus m_fileStatus = FileStatus.Close;
        private byte[] m_flag = Encoding.ASCII.GetBytes("\r\n\r\n");
        private FileStream m_fs;
        public string m_rawFileName;
        private ArrayList m_tempBoundary = new ArrayList();
        private ArrayList m_tempContent = new ArrayList();
        private ArrayList m_templist1 = new ArrayList();
        private ArrayList m_templist2 = new ArrayList();
        private string m_tempPath;

        public void Dispose()
        {
            this.m_boundaryFlag = null;
            this.m_flag = null;
            this.m_fs = null;
            this.m_contentTextBody = null;
            this.m_tempBoundary = null;
            this.m_tempContent = null;
            this.m_templist1 = null;
            this.m_templist2 = null;
        }

        private string[] GetFileContent(string m_contentHeader)
        {
            m_contentHeader = m_contentHeader.Replace("\r\n", ";");
            return m_contentHeader.Split(new char[] { ';' });
        }

        private string GreateFileStream(string m_fileName)
        {
            m_fileName = m_fileName.Replace("\"", "");
            if ((m_fileName.Length <= 0) || (m_fileName.IndexOf(".") < 0))
            {
                return string.Empty;
            }
            string str = m_fileName.Substring(m_fileName.LastIndexOf("."));
            this.m_rawFileName = m_fileName.Substring(m_fileName.IndexOf("=") + 1);
            string path = Guid.NewGuid().ToString();
            if ((this.m_tempPath == string.Empty) || !Directory.Exists(this.m_tempPath))
            {
                this.m_tempPath = Path.GetTempPath();
            }
            string str3 = Path.Combine(this.m_tempPath, path + str);
            Hashtable hashtable = (Hashtable) this.m_content.Items["Webb_Upload_FileList"];
            if (hashtable != null)
            {
                hashtable.Add(Path.GetFileNameWithoutExtension(path), path + str);
            }
            else
            {
                hashtable = new Hashtable();
                hashtable.Add(Path.GetFileNameWithoutExtension(path), str3);
            }
            this.m_content.Items["Webb_Upload_FileList"] = hashtable;
            this.m_fs = new FileStream(str3, FileMode.Create);
            this.m_content.Items["Webb_Upload_Stream"] = this.m_fs;
            return (path + str);
        }

        public void SetBoundaryFlag(byte[] i_boundaryFlag)
        {
            this.m_boundaryFlag = new byte[i_boundaryFlag.Length];
            i_boundaryFlag.CopyTo(this.m_boundaryFlag, 0);
            this.i_boundarySize = this.m_boundaryFlag.Length;
        }

        public void SetHttpContent(HttpContext i_content)
        {
            this.m_content = i_content;
        }

        public void SetTempPath(string i_tempPath)
        {
            this.m_tempPath = i_tempPath;
        }

        public void TransactReadData(byte[] m_readData)
        {
            if (((m_readData.Length != 0) && (this.m_content != null)) && (this.m_boundaryFlag.Length != 0))
            {
                long num = 0L;
                long length = m_readData.Length;
                bool flag = true;
                while (num < length)
                {
                    if ((m_readData[(int) ((IntPtr) num)] != 13) && !flag)
                    {
                        goto Label_0346;
                    }
                    flag = false;
                    while ((this.m_boundaryIndex < this.i_boundarySize) && (num < length))
                    {
                        if (m_readData[(int) ((IntPtr) num)] != this.m_boundaryFlag[this.m_boundaryIndex])
                        {
                            break;
                        }
                        this.m_templist2.Add(m_readData[(int) ((IntPtr) num)]);
                        this.m_boundaryIndex++;
                        num += 1L;
                    }
                    if (this.m_boundaryIndex != this.i_boundarySize)
                    {
                        goto Label_0346;
                    }
                    if ((num + 2L) < length)
                    {
                        this.m_templist2.Add(m_readData[(int) ((IntPtr) num)]);
                        num += 1L;
                        this.m_templist2.Add(m_readData[(int) ((IntPtr) num)]);
                        num += 1L;
                    }
                    this.m_contentTextBody.AddRange(this.m_templist2);
                    this.m_templist2.Clear();
                    if ((this.m_fileStatus == FileStatus.Open) && (this.m_fs != null))
                    {
                        this.m_fs.Flush();
                        this.m_fs.Close();
                        this.m_fs = null;
                        this.m_fileStatus = FileStatus.Close;
                    }
                    goto Label_033F;
                Label_012F:
                    if (this.m_flag[this.m_contentIndex] != m_readData[(int) ((IntPtr) num)])
                    {
                        goto Label_0177;
                    }
                    this.m_templist1.Add(m_readData[(int) ((IntPtr) num)]);
                    this.m_contentIndex++;
                    num += 1L;
                Label_016A:
                    if ((num < length) && (this.m_contentIndex < 4))
                    {
                        goto Label_012F;
                    }
                Label_0177:
                    if (this.m_contentIndex == 4)
                    {
                        this.m_tempContent.AddRange(this.m_flag);
                        this.m_templist1.Clear();
                        byte[] array = new byte[this.m_tempContent.Count];
                        this.m_tempContent.CopyTo(array);
                        string str = HttpContext.Current.Request.ContentEncoding.GetString(array);
                        if (str.IndexOf("\"; filename=\"") < 0)
                        {
                            this.m_contentTextBody.AddRange(this.m_tempContent);
                            this.m_fileStatus = FileStatus.Close;
                        }
                        else
                        {
                            string[] fileContent = this.GetFileContent(str);
                            string str2 = this.GreateFileStream(fileContent[2]);
                            StringBuilder builder = new StringBuilder();
                            builder.Append(fileContent[0] + ";" + fileContent[1] + "\r\n\r\n" + fileContent[3] + ";" + fileContent[2] + "; " + "filePath=\"" + str2 + "\"");
                            this.m_contentTextBody.AddRange(Encoding.UTF8.GetBytes(builder.ToString().ToCharArray()));
                            builder.Remove(0, builder.Length);
                            this.m_fileStatus = FileStatus.Open;
                        }
                        this.m_tempContent.Clear();
                        this.m_templist1.Clear();
                        this.m_contentIndex = 0;
                        this.m_boundaryIndex = 0;
                        goto Label_0346;
                    }
                    if (num == length)
                    {
                        goto Label_0346;
                    }
                    if (this.m_templist1.Count > 0)
                    {
                        this.m_tempContent.AddRange(this.m_templist1);
                        this.m_templist1.Clear();
                        this.m_contentIndex = 0;
                    }
                    this.m_tempContent.Add(m_readData[(int) ((IntPtr) num)]);
                    num += 1L;
                Label_033F:
                    if (num < length)
                    {
                        goto Label_016A;
                    }
                Label_0346:
                    if (num >= length)
                    {
                        return;
                    }
                    if (m_readData[(int) ((IntPtr) num)] == 13)
                    {
                        long num3 = num;
                        int index = 0;
                        while ((index < this.i_boundarySize) && (num < length))
                        {
                            if (m_readData[(int) ((IntPtr) num)] != this.m_boundaryFlag[index])
                            {
                                break;
                            }
                            index++;
                            num += 1L;
                        }
                        if ((this.m_fileStatus == FileStatus.Open) && (this.m_templist2.Count > 0))
                        {
                            byte[] buffer2 = new byte[this.m_templist2.Count];
                            this.m_templist2.CopyTo(buffer2);
                            this.m_fs.Write(buffer2, 0, this.m_templist2.Count);
                            this.m_templist2.Clear();
                            buffer2 = null;
                        }
                        if (index == this.i_boundarySize)
                        {
                            this.m_boundaryIndex = 0;
                            num = num3;
                            continue;
                        }
                        num = num3;
                    }
                    if (this.m_fileStatus == FileStatus.Open)
                    {
                        if (this.m_templist2.Count > 0)
                        {
                            byte[] buffer3 = new byte[this.m_templist2.Count];
                            this.m_templist2.CopyTo(buffer3);
                            this.m_fs.Write(buffer3, 0, buffer3.Length);
                            buffer3 = null;
                        }
                        this.m_fs.WriteByte(m_readData[(int) ((IntPtr) num)]);
                    }
                    else
                    {
                        if (this.m_tempBoundary.Count > 0)
                        {
                            this.m_contentTextBody.AddRange(this.m_templist2);
                        }
                        this.m_contentTextBody.Add(m_readData[(int) ((IntPtr) num)]);
                    }
                    this.m_templist1.Clear();
                    this.m_templist2.Clear();
                    this.m_boundaryIndex = 0;
                    this.m_contentIndex = 0;
                    num += 1L;
                }
            }
        }

        public ArrayList ContentTextBody
        {
            get
            {
                return this.m_contentTextBody;
            }
        }

        public enum FileStatus : byte
        {
            Close = 1,
            Open = 0
        }
    }
}

